package com.app.mymusic.widget;

import android.content.Context;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.EditText;

import com.app.mymusic.R;

/**
 * Created by Administrator on 2016/7/25.
 */
public class SearchEditText extends EditText {
    private  Context context;
    private Drawable serDrawable;
    private OnEditTextListener listener;
    public void setOnEditTextListener(OnEditTextListener listener)
    {
        this.listener=listener;
    }

    public SearchEditText(Context context) {
        this(context,null);
        init();
    }


    public SearchEditText(Context context, AttributeSet attrs) {
        this(context,attrs,android.R.attr.editTextStyle);

    }

    public SearchEditText(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        serDrawable=getResources().getDrawable(R.drawable.search);
        setCompoundDrawablesWithIntrinsicBounds(null,null,serDrawable,null);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if(serDrawable!=null&&event.getAction()==MotionEvent.ACTION_UP)
        {
            int eventX= (int) event.getRawX();
            int eventY=(int)event.getRawY();
            Rect r=new Rect();
            getGlobalVisibleRect(r);
            r.left=r.right-50;
            if(r.contains(eventX,eventY))
            {
                listener.OnSearch();
            }
        }
        return super.onTouchEvent(event);
    }

    @Override
    protected void finalize() throws Throwable {
        super.finalize();
    }

    public  interface  OnEditTextListener
    {
        void OnSearch();
    }
}
